<template>
  <div :class="$style.wrap">
    <ContentWrap>
      <div :class="$style.head">
        <h1 :class="$style.title">评分表编辑器</h1>
        <div :class="$style.button">
          <Chapter :questionnaire-id="$route.params.id" :index="formData.questionnaireChapterDTOList.length + 1" @on-success="getDetail"></Chapter>
          <PreviewDialog :id="$route.params.id"></PreviewDialog>
          <!-- <Button type="primary">保存</Button> -->
          <Button type="primary" @click="releaseHandle">发布</Button>
        </div>
      </div>
      <div :class="$style.content">
        <FormContainer :column="1">
          <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
            <el-form-item prop="title" label="评分表名称">
              <el-input v-model="formData.title" type="textarea" :autosize="{minRows:1}" tip maxlength="100" placeholder="请输入评分表名称"></el-input>
            </el-form-item>
            <el-form-item prop="questionnaireChapterDTOList" label="标题">
              <ChapterList :chapter-list="formData.questionnaireChapterDTOList" :questionnaire-id="$route.params.id" @on-refresh="getDetail">
                <div style="text-align: center;padding: 10px;">
                  <Chapter :questionnaire-id="$route.params.id" :index="formData.questionnaireChapterDTOList.length + 1" style="margin:0;" @on-success="getDetail"></Chapter>
                </div>
              </ChapterList>
            </el-form-item>
          </el-form>
        </FormContainer>
      </div>
    </ContentWrap>
  </div>
</template>
<script>
import ContentWrap from '@/components/ContentWrap'
import FormContainer from '@/components/FormContainer'
import Chapter from './components/chapter'
import ChapterList from './components/chapterList'
import PreviewDialog from './components/previewDialog'

import { charNum, chara, charA, charCn } from './common'
import { questionnaireDetailByIdSetAnswer, questionnaireRelease } from '@/api/questionnaire'
export default {
  name: 'InsQuestionnaire',
  components: {
    ContentWrap,
    FormContainer,
    Chapter,
    ChapterList,
    PreviewDialog
  },
  data() {
    // 验证问题
    const validatorChapterList = (r, v, c) => {
      c()
    }
    return {
      formData: {
        title: '',
        questionnaireChapterDTOList: [],
        id: '',
        questionnaireSort: 1,
        state: 0
      },
      formRules: {
        title: [{ required: true, message: '请输入评分表名称', trigger: 'change' }],
        questionnaireChapterDTOList: [{ required: true, validator: validatorChapterList, trigger: 'change' }]
      },
      detail: {}
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    // 根据id获取详情
    getDetail() {
      const { id } = this.$route.params
      questionnaireDetailByIdSetAnswer(id).then(res => {
        this.detail = res.data
        this.initFormData()
      })
    },
    // 发布
    releaseHandle() {
      return this.$msgbox({
        title: '提示',
        message: '确认发布？',
        type: 'warning',
        showCancelButton: true
      }).then(() => {
        return questionnaireRelease(this.$route.params.id).then(res => {
          this.$message.success('发布成功！')
          this.$closePage()
        })
      })
    },
    initFormData() {
      const keys = Object.keys(this.formData)
      if (keys.length <= 0) return
      for (const k of keys) {
        this.$set(this.formData, k, this.detail[k])
      }
    },
    // 章节序号
    charNum,
    charA,
    chara,
    charCn
  }
}
</script>
<style lang='scss' module>
.wrap{
  .head {
    position: relative;
    overflow: hidden;
    height: 80px;
    line-height: 80px;
    margin-top: -20px;
    .title{
      color: #333;
      font-size: 20px;
      margin: 0;
      text-align: center;
    }
    .button{
      position: absolute;
      top: 0;
      right: 0;
    }
  }
  .content{
  }
}
</style>
